package com.dlc.beijingrfid.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;
import butterknife.BindView;
import com.dlc.commonlibrary.adapter.BaseRecyclerAdapter;
import com.dlc.commonlibrary.utils.RecyclerViewUtil;
import com.dlc.commonlibrary.utils.TimeFormats;
import com.dlc.commonlibrary.widget.TitleBar;
import com.dlc.beijingrfid.R;
import com.dlc.beijingrfid.base.BaseActivity;
import com.dlc.beijingrfid.net.bean.WashRecordBean;
import com.dlc.beijingrfid.net.protocol.StockProto;
import com.trello.rxlifecycle.android.ActivityEvent;
import java.text.SimpleDateFormat;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class WashCountActivity extends BaseActivity {

    private static final String EXTRA_RFID = "rfid";

    @BindView(R.id.title_bar) TitleBar mTitleBar;
    @BindView(R.id.tv_number) TextView mTvNumber;
    @BindView(R.id.recycler_view) RecyclerView mRecyclerView;

    private String mRfid;
    private InnerAdapter mInnerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        resolveIntent();

        mTitleBar.leftExit(this);
        
        mTvNumber.setText("0");
        initRecyclerView();

        getWashRecords();
    }

    private void initRecyclerView() {
        mInnerAdapter = new InnerAdapter();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        RecyclerViewUtil.setDefaultDivider(mRecyclerView, linearLayoutManager);
        mRecyclerView.setAdapter(mInnerAdapter);
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_wash_count;
    }

    public static Intent newIntent(Activity activity, String rfid) {
        Intent intent = new Intent(activity, WashCountActivity.class);
        intent.putExtra(EXTRA_RFID, rfid);
        return intent;
    }

    private void resolveIntent() {

        Intent intent = getIntent();
        mRfid = intent.getStringExtra(EXTRA_RFID);
    }

    public void getWashRecords() {
        StockProto.get()
            .wash_record(mRfid, 0, null)
            .subscribeOn(Schedulers.io())
            .compose(this.<WashRecordBean>bindUntilEvent(ActivityEvent.DESTROY))
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Subscriber<WashRecordBean>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onNext(WashRecordBean washRecordBean) {
                    mTvNumber.setText(String.valueOf(washRecordBean.data.totalcount));
                    mInnerAdapter.setNewData(washRecordBean.data.list);
                }
            });
    }

    private static class InnerAdapter
        extends BaseRecyclerAdapter<WashRecordBean.DataBean.ListBean> {

        private SimpleDateFormat mDateFormat = TimeFormats.FORMAT_4;
        private SimpleDateFormat mTimeFormat = TimeFormats.FORMAT_3;

        @Override
        public int getItemLayoutId(int viewType) {
            return R.layout.item_wash_count_cost;
        }

        @Override
        public void onBindViewHolder(CommonHolder holder, int position) {
            WashRecordBean.DataBean.ListBean item = getItem(position);
            long time = item.ctime * 1000L;
            holder.setText(R.id.tv_date, mDateFormat.format(time));
            holder.setText(R.id.tv_time, mTimeFormat.format(time));
        }
    }
}
